package gui;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SpinnerNumberModel;

import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.Dimension;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Aplicacion.MP3Player;
import Aplicacion.Variables_globales;
import Entidades.Paises;
import Entidades.Posesion;

import data.Database;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.LinkedList;

public class DefenderGUI implements WindowListener, ChangeListener, ActionListener {
	private final Database db = new Database();
	
	private LinkedList<Paises> paises;
	private LinkedList<Posesion> posesion;
	
	private int id_pais1;
	private int id_pais2;
	private int soldados1;
	private int soldados2;
	
	private JFrame frame;
	private JLabel pais2;
	private JComboBox listaPaises;
	private JSpinner numSoldados;

	public DefenderGUI(int id_pais, String nombre, int x, int y) {
		id_pais2 = id_pais;
		this.initialize();
		
		frame.setLocation(x, y);
		pais2.setText(nombre);
		
		paises	 = db.getPaises();
		posesion = db.getPosesion();
		for (Posesion pos: posesion) {
			if (pos.getIDPais() == id_pais) {
				soldados2 = pos.getSoldados();
			}
			if (pos.getIDJugador() == Variables_globales.LISTA_JUGADORES.get(Variables_globales.JUGADOR_ACTUAL).getIDJugador()) {
				for (Paises p: paises) {
					if (pos.getIDPais() == p.getIdPais() && pos.getSoldados() > 1) {
						listaPaises.addItem(p.getNombre());
					}
				}
			}
		}
		
		frame.setVisible(true);
	}

	private void initialize() {
		frame = new JFrame();
		frame.setAlwaysOnTop(true);
		frame.setTitle("Defender");
		frame.setResizable(false);
		frame.setSize(450, 100);
		frame.addWindowListener(this);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		
		JLabel lblMoverDesde = new JLabel("Mover desde ");
		panel.add(lblMoverDesde);
		
		listaPaises = new JComboBox();
		listaPaises.setPreferredSize(new Dimension(200, 27));
		listaPaises.addActionListener(this);
		panel.add(listaPaises);
		
		JLabel lblHasta = new JLabel("hasta");
		panel.add(lblHasta);
		
		pais2 = new JLabel("Pais 2");
		pais2.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		panel.add(pais2);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
		
		JLabel lblMover = new JLabel("Mover");
		panel_1.add(lblMover);
		
		numSoldados = new JSpinner();
		numSoldados.setPreferredSize(new Dimension(100, 28));
		numSoldados.addChangeListener(this);
		numSoldados.setEnabled(false);
		panel_1.add(numSoldados);
		
		JLabel lblSoldados = new JLabel("Soldados");
		panel_1.add(lblSoldados);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		numSoldados.setEnabled(false);
		paises = db.getPaises();
		for (Paises p: paises) {
			if (p.getNombre().equalsIgnoreCase(listaPaises.getSelectedItem().toString())) {
				for (Posesion pos: db.getPosesion()) {
					if (pos.getIDPais() == p.getIdPais() && pos.getSoldados() > 1) {
						try {
							numSoldados.setModel(new SpinnerNumberModel(0, 0, (db.getSoldadosDisponiblesEnPais(p.getIdPais())-1), 1));
						} catch (IllegalArgumentException ex) {
							frame.dispose();
						}
						soldados1 = db.getSoldadosDisponiblesEnPais(p.getIdPais());
						id_pais1 = p.getIdPais();
						numSoldados.setEnabled(true);
					}
				}
			}
		}
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		int tmp = Integer.parseInt(numSoldados.getValue().toString());
		db.moverSoldados(id_pais1, id_pais2, (soldados1-tmp), (soldados2+tmp));
		
		try {
			MP3Player WAV = new MP3Player();
			WAV.playDaemon(this.getClass().getResource("/Imagenes/clickMenu.mp3").toString());
		} catch (IllegalThreadStateException ex) {}
	}
	
	@Override
	public void windowClosing(WindowEvent e) {
		Variables_globales.PAUSA = false;
	}
	
	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}
}
